package com.woniuxy;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

class Company {
    private int id; //l公司编号
    private String name;//公司名
    private String boss;//公司老板
    private int year; //公司创立年份

    public Company(int id, String name, String boss, int year) {
        this.id = id;
        this.name = name;
        this.boss = boss;
        this.year = year;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBoss() {
        return boss;
    }

    public void setBoss(String boss) {
        this.boss = boss;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    @Override
    public String toString() {
        return "Company{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", boss='" + boss + '\'' +
                ", year=" + year +
                '}';
    }
}

public class StreamDemo {
    public static void main(String[] args) {
        List<Company> list = new ArrayList<Company>();
        list.add(new Company(1, "阿里巴巴", "马云", 1999));
        list.add(new Company(2, "淘宝", "马云", 2003));
        list.add(new Company(3, "华为", "任正非", 1987));
        list.add(new Company(4, "小米", "雷军", 2010));
        list.add(new Company(5, "雷人科技", "邓强", 2018));
        list.add(new Company(6, "蜗牛学苑", "邓强", 2015));
        list.add(new Company(7, "腾讯", "马化腾", 1998));
        list.add(new Company(8, "百度", "李彦宏", 2000));
        list.add(new Company(9, "bilibili","陈冠希",2006)
                list.add(new Company(1, "阿里巴巴", "马云", 1999));
        list.add(new Company(2, "淘宝", "马云", 2003));
        list.add(new Company(3, "华为", "任正非", 1987));
        list.add(new Company(4, "小米", "雷军", 2010));
        list.add(new Company(5, "雷人科技", "邓强", 2018));
        list.add(new Company(6, "蜗牛学苑", "邓强", 2015));
        list.add(new Company(7, "腾讯", "马化腾", 1998));
        list.add(new Company(8, "百度", "李彦宏", 2000));
        list.add(new Company(9, "bilibili","陈冠希",2006)


        //1、找出马云创建的公司,并输出
        list.stream().
                filter(company -> "马云".equals(company.getBoss()))
                .forEach(company -> System.out.println(company.getName()));
        System.out.println("-----------------------------------------------");
        //2、邓强创建了几家公司?
        long count = list.stream().
                filter(company -> "邓强".equals(company.getBoss()))
                .count();
        System.out.println(count);
        System.out.println("-----------------------------------------------");
        //3、马云历次创建公司的最早年份


        Optional<Integer> earliestYear = list.stream()
                .filter(company -> "马云".equals(company.getBoss()))
                .map(Company::getYear) // 获取创建年份
                .sorted() // 对年份进行排序
                .findFirst(); // 获取最早的年份
        System.out.println(earliestYear.get());
        System.out.println("-----------------------------------------------");

        //4、给所有公司按创建年份降序输出
        list.stream()
                .sorted(Comparator.comparing(Company::getYear))
                .forEach(company -> System.out.println(company.getYear()));
        System.out.println("-----------------------------------------------");
        //5、把所有老板名字收集到一个集合中,且不可重复
        List<String> bosses = list.stream()
                .map(Company::getBoss)
                .distinct().toList();
        for (int i = 0; i < bosses.size(); i++) {
            System.out.println(bosses.get(i));

        }

    }
}